function q = nextindex(pai,p)
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here
if(isnan(p))
    q=0;
    return;
end
p = dec2bin(p);
length_p = length(p);
%disp(p);
tmp = zeros(1,length(pai));
for m = 1:(length(pai)-length_p)
    tmp(1,m) = 0;
end
for n = 1:length_p
    tmp(1,n+length(pai)-length_p) = str2num(p(n));
end
x = 0;
for i = 1:length(tmp)
    x = x + tmp(pai(i)+1)*2^(i-1);
end

x = x + 1;
%x = bitreversed(x,length_p);
x = dec2bin(x);
if(length(x)>length(pai))
    x = x(2:length(x));
end


tmpx = zeros(1,length(pai));
for m = 1:(length(pai)-length(x))
    tmpx(1,m) = 0;
end
for n = 1:length(x)
    tmpx(1,n+length(pai)-length(x)) = str2num(x(n));
end


q = 0;
for j = 1:length(pai)
    q = q + tmpx(j)*2^(pai(j));
end

end

